﻿using System;
using Microsoft.Practices.Prism.ViewModel;
using Google.GData.Calendar;
using Google.GData.Documents;
using System.IO;
using System.Linq;
using System.Windows;
using Google.Documents;
using Google.GData.Client;
using System.Text;

namespace CharacterCreation.Data.Services {
    public class GoogleService : NotificationObject {

        private DocumentsService createDocumentService() {
            DocumentsService service = new DocumentsService("CharacterCreation");
            service.setUserCredentials("spelersgroep37@gmail.com", "!spelersgroep01");
            return service;
        }

        public DocumentsFeed DocumentsFeed {
            get {
                try {
                    DocumentsListQuery query = new DocumentsListQuery();
                    DocumentsFeed feed = createDocumentService().Query(query);
                    return feed;
                }
                catch (Exception ex) {
                }
                return null;
            }
        }

        public void UploadDocument(string path) {
            try {
                FileInfo fInfo = new FileInfo(path);
                if (fInfo.Exists) {
                    DocumentsRequest request = new DocumentsRequest(new RequestSettings("CharacterCreation", "spelersgroep37@gmail.com", "!spelersgroep01"));
                    var entry = request.GetDocuments().Entries.FirstOrDefault(e => e.Title == fInfo.Name);
                    if (entry != null) {
                        entry.DocumentEntry.Delete();
                    }

                    DocumentEntry newEntry = createDocumentService().UploadDocument(path, fInfo.Name);
                }
            }
            catch (Exception ex) {
            }
        }

        public bool DownloadDocument(string documentName) {
            try {
                DocumentsRequest request = new DocumentsRequest(new RequestSettings("CharacterCreation", "spelersgroep37@gmail.com", "!spelersgroep01"));
                Feed<Document> everything = request.GetEverything();

                foreach (Document item in everything.Entries) {
                    if (item.Title == documentName) {
                        Stream stream = request.Download(item, "txt");
                        StreamReader reader = new StreamReader(stream);
                        File.WriteAllBytes("C:\\DataBases\\" + item.Title, new UTF8Encoding().GetBytes(reader.ReadToEnd()));
                        return true;
                    }
                }
            }
            catch {
                return false;
            }
            return false;
        }

        private CalendarService createCalendarService() {
            CalendarService service = new CalendarService("CharacterCreation");
            service.setUserCredentials("spelersgroep37@gmail.com", "!spelersgroep01");
            return service;
        }

        /// <summary>
        /// Gets or sets the CalendarFeed
        /// </summary>
        public CalendarFeed CalendarFeed {
            get {
                try {
                    if (_CalendarFeed == null) {
                        CalendarQuery query = new CalendarQuery();
                        query.Uri = new Uri("https://www.google.com/calendar/feeds/default/owncalendars/full");
                        _CalendarFeed = (CalendarFeed)createCalendarService().Query(query);
                    }
                    return _CalendarFeed;
                }
                catch (Exception ex) {
                }

                return null;
            }
            set {
                _CalendarFeed = value;
                RaisePropertyChanged("CalendarFeed");
            }
        }
        private CalendarFeed _CalendarFeed;




        /// <summary>
        /// Gets or sets the EventFeed
        /// </summary>
        public EventFeed EventFeed {
            get {
                try {
                    if (_EventFeed == null) {
                        EventQuery query = new EventQuery();
                        query.Uri = new Uri("https://www.google.com/calendar/feeds/spelersgroep37@gmail.com/private/full");
                        _EventFeed = (EventFeed)createCalendarService().Query(query);
                    }
                    return _EventFeed;
                }
                catch { }

                return null;
            }
            set {
                _EventFeed = value;
                RaisePropertyChanged("EventFeed");
            }
        }
        private EventFeed _EventFeed;



        public GoogleService() {



        }
    }
}
